package com.scuec.trainingmanagementsys.service.impl;

import com.scuec.trainingmanagementsys.entity.Student;
import com.scuec.trainingmanagementsys.entity.Teacher;
import com.scuec.trainingmanagementsys.mapper.TeacherMapper;
import com.scuec.trainingmanagementsys.service.TeacherService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Resource
    private TeacherMapper teacherMapper;

    // 整表查询
    @Override
    public List<Teacher> getAllTeachers() {
        return teacherMapper.getAllTeachers();
    }

    // 根据工号查询
    @Override
    public Teacher getByNumber(String number) {
        return StringUtils.hasText(number) ? teacherMapper.getByNumber(number) : null;
    }

    // 姓名模糊查询
    @Override
    public List<Teacher> searchByName(String name) {
        if (!StringUtils.hasText(name)) return null;
        return teacherMapper.searchByName("%" + name + "%");
    }

    // 学院查询
    @Override
    public List<Teacher> getByCollege(String college) {
        return StringUtils.hasText(college) ? teacherMapper.getByCollege(college) : null;
    }

    // 多条件查询（自动处理模糊匹配）
    @Override
    public List<Teacher> searchByNameAndCollege(String name, String college) {
        if (!StringUtils.hasText(name) || !StringUtils.hasText(college)) return null;
        return teacherMapper.searchByNameAndCollege("%" + name + "%", college);
    }

    // 删除单条记录
    @Override
    public int deleteByNumber(String number) {
        return StringUtils.hasText(number) ? teacherMapper.deleteByNumber(number) : 0;
    }

    // 多选删除（过滤空值）
    @Override
    public int deleteBatchByNumbers(List<String> numbers) {
        if (numbers == null || numbers.isEmpty()) return 0;
        numbers.removeIf(StringUtils::isEmpty); // 过滤空工号
        return numbers.isEmpty() ? 0 : teacherMapper.deleteBatchByNumbers(numbers);
    }

    // 插入记录（简单校验）
    @Override
    public int insertTeacher(Teacher teacher) {
        if (teacher == null || !StringUtils.hasText(teacher.getTeacherNumber())) {
            return 0; // 工号不能为空
        }
        return teacherMapper.insertTeacher(teacher);
    }

    // 修改记录（校验工号存在性，可扩展）
    @Override
    public int updateTeacher(Teacher teacher) {
        if (teacher == null || !StringUtils.hasText(teacher.getTeacherNumber())) {
            return 0;
        }
        // 可在此处添加查询校验（如工号是否存在）
        return teacherMapper.updateTeacher(teacher);
    }

    // 根据工号和密码查询教师
    @Override
    public Teacher getByNumberAndPassword(String number, String password) {
        if (!StringUtils.hasText(number) || !StringUtils.hasText(password)) {
            return null;
        }
        return teacherMapper.getByNumberAndPassword(number, password);
    }

    // 获取教师总数
    @Override
    public int getTeacherCount() {
        return teacherMapper.getTeacherCount();
    }

    @Override
    public Teacher getTeacherByNumberAndPassword(String teacherNumber, String password) {
        return teacherMapper.getByNumberAndPassword(teacherNumber, password);
    }

    @Override
    public Integer getTeacherIdByNumber(String teacherNumber) {
        return teacherMapper.getTeacherIdByNumber(teacherNumber);
    }


    @Override
    public Teacher findByTeacherNumber(String teacherNumber) {
        return teacherMapper.findByTeacherNumber(teacherNumber);
    }

    @Override
    public Teacher getTeacherProfile(String teacherNumber) {
        return teacherMapper.getTeacherProfile(teacherNumber);
    }

    // 新增方法实现，用于更新教师信息
    @Override
    public int updateTeacherByNumber(String teacherNumber, Map<String, Object> teacherData) {
        Teacher teacher = new Teacher();
        teacher.setTeacherNumber(teacherNumber);
        teacher.setTeacherName((String) teacherData.get("teacherName"));
        teacher.setCollege((String) teacherData.get("college"));
        teacher.setEmail((String) teacherData.get("email"));
        teacher.setPhone((String) teacherData.get("phone"));

        if (teacherData.containsKey("teacherPassword") &&
                teacherData.get("teacherPassword") != null &&
                !"".equals(teacherData.get("teacherPassword"))) {
            teacher.setTeacherPassword((String) teacherData.get("teacherPassword"));
        }

        return teacherMapper.updateTeacher(teacher);
    }
}

